{% macro selectinputdict(name, values, value='') -%}
    <select name="{{ name }}" id="{{ name }}">
    {% for k,v in values.items() %}
        <option value="{{ k }}"  {{ 'selected' if value==k }}>{{ v }}</option>
    {% endfor %}
    </select>
{%- endmacro %}

{% extends "layout.html" %}


{% block body %}
<style>
.table-condensed tr td {padding-top: 0px  !important;padding-bottom: 0px  !important; } {# On condense encore plus #}
#TblTaxo td:nth-child(1) {font-size: small;padding: 0}
</style>
<h3>自动分类：设置 </h3>
<form class="form-horizontal" method="post" enctype=multipart/form-data>
<div class="row">
  <div class="col-md-6">
  <div class="form-group">
    <label class="col-sm-4 control-label" >Classification Method</label>
    <div class="col-sm-8">
{#        {{ selectinputdict("Methode",{'':'','randomforest':"Random Forest",'svm':"SVM"},data.Methode) }}#}
        {{ selectinputdict("Methode",{'randomforest':"Random Forest"},data.Methode) }}
    </div>
  </div><!-- /form-group -->

  <div class="form-group">
    <label class="col-sm-4 control-label" >OBJECTS to PREDICT</label>
    <div class="col-sm-8">
        {{ selectinputdict("Perimeter",{'nmc':"NOT Validated Objects (Unclassified or Predicted)",'all':"ALL Objects (for confusion matrix computation only)"},data.Perimeter) }}
    </div>
  </div><!-- /form-group -->
<div class="form-group">
    <label class="col-sm-4 control-label" >Keep log of previous automatic classification</label>
    <div class="col-sm-4">
        {{ selectinputdict("keeplog",{'no':"No",'yes':"Yes"},"no") }}
    </div> <!-- col-sm-4 -->
  </div><!-- /form-group -->
<div class="form-group">
    <label class="col-sm-4 control-label" >Limit number of objects by category from learning set </label>
    <div class="col-sm-4">
        <input type="text" id="learninglimit" name="learninglimit" value="{{ data.learninglimit }}" size="2">
    </div> <!-- col-sm-4 -->
  </div><!-- /form-group -->
</div>  <div class="col-md-6">
    <p style="padding-left: 100px;">
    <button type="submit" class="btn btn-primary">START Automatic PREDICTION of IDs</button>
        </p>
<div class="panel  panel-default">    <ul>
<li>The prediction (automatic classification) will never replace the manual validation
<li>Default settings for the classification have been validated by experts. User should keep them in most cases
<li>Classification settings are recorded in Ecotaxa for the next prediction
<li>Categories selected for the prediction should be correctly validated by expert
<li>The experience shows that it is often more efficient to automatically classify into a limited number of categories and then validate in detail using more categories
</ul></div>
</div>
</div> {#ROW#}

<script>
    function Applyfilter(){
        $('#TblTaxo input').prop( 'checked', false );
        var Limit=parseInt($('#selectlimit').val());
        var LimitType=$('#selecttype').val();
        $('#TblTaxo input').each(function(){
            if((LimitType=='N')&&($(this).data("nbr")>=Limit))
                $(this).prop( 'checked', true );
            if((LimitType=='P')&&($(this).data("pct")>=Limit))
                $(this).prop( 'checked', true );
        });
    }

    function ComputeCritVar() {
        var cv="";
        $('#TblVar input').each(function (){
           if($(this).prop("checked")){
               if(cv!="")
                   cv+=",";
               cv+=$(this).data("var");
           }
        });
        $('#CritVar').val(cv);
    }
    function CheckBasedOnText() {
        var cv=$('#CritVar').val();
        var cvl=cv.split(",");
        //console.log(cvl);
        $('#TblVar input').each(function (){
           if($(this).prop("checked",cvl.indexOf($(this).data("var"))>=0)){
               if(cv!="")
                   cv+=",";
               cv+=$(this).data("var");
           }
        });
    }
    function UncheckNonPertinent(){
        var perimetre=$('#Perimeter').val();
        $('#TblVar tr').each(function (){
           var v=parseInt($(this).find("td:nth-child(3)").text() ) || 0;
           if(v<=75) {# Clean si moins de 25% de données sources remplis #}
               $(this).find("input").prop("checked",false);
           v=parseInt($(this).find("td:nth-child(4)").text() ) || 0;
           if(v<=1) {# Clean si pas assez de valeur distincte #}
               $(this).find("input").prop("checked",false);
           if (perimetre=="all")
            v=parseInt($(this).find("td:nth-child(5)").text() ) || 0;
           else
            v=parseInt($(this).find("td:nth-child(6)").text() ) || 0;
           if(v<=75) {#  Clean si moins de 25% de données cibles remplis #}
               $(this).find("input").prop("checked",false);
        });

        ComputeCritVar();
    }
</script>

<table>
<tr><td valign="top">
{# ----------- Partie Gestion des Taxon --------------------- #}
select Categories with more than <input type="text" id="selectlimit" value="5" size="2">
    <select id="selecttype">
        <option value="N">Objects</option>
        <option value="P">% of objects</option>
    </select>
<input type="button" class="btn" value="Select" onclick="Applyfilter()"><br>
选择 <a name="tbltop" href="#tbltop" onclick="$('#TblTaxo input').prop( 'checked', true )">ALL</a>
    / <a href="#tbltop" onclick="$('#TblTaxo input').prop( 'checked', false );">None</a>
<table class="table table-bordered table-condensed" style="width: auto" id="TblTaxo">
    <tr><th colspan="2">(id) 选择</th><th width="200" style="text-align: center;">Category</th><th style="text-align: center;">Nbr</th><th  style="text-align: center;">%</th></tr>
    {%   for r in g.TaxoList %}
        <tr><td>({{ r[0] }})</td><td><input type="checkbox" value="Y" name="taxo{{ r[0] }}" data-nbr="{{ r[2] }}" data-pct="{{ r[3] }}" {{ r[4] }}></td><td>{{ r[1] }} </td><td class="rightfixedfont">{{ r[2] }}</td><td class="rightfixedfont">{{ r[3] }} %</td></tr>
    {% endfor %}
</table>
</td><td width="20"></td><td valign="top">
{# ----------- Partie Gestion des Variable --------------------- #}
Using variables :<br>
<input type="text" name="CritVar" id="CritVar" value="{{ data.CritVar }}" style="width: 400px;"><br>

选择 <a name="tbltop2" href="#tbltop2" onclick="$('#TblVar input').prop( 'checked', true );ComputeCritVar();return false;">ALL</a>
    / <a href="#tbltop2" onclick="$('#TblVar input').prop( 'checked', false );ComputeCritVar();return false;">None</a>
    / <a href="#tbltop2" onclick="CheckBasedOnText();return false;" title="Usefull to copy setting from another project" >Text field content</a>
    / <a href="#tbltop2" onclick="UncheckNonPertinent();return false;" title="uncheck distinct<1 and %<75%" >Uncheck non pertinent variable</a>
 - <a href="#variable-help" data-toggle="collapse" class="help">Variable help</a>
<div id=variable-help class="panel panel-default collapse " style="margin-left: 0px;margin-top: 5px;width: 500px" >
<div style="margin:5px;">
<span class=" glyphicon glyphicon-info-sign"></span> <ul><li>Objects variable are used for prediction. Only variables with more than one distinct value in the learning set are available.
<li>Non pertinent variables are variable with less than 75% populated in the learning set or in the target scope. They can be automatically unchecked using the link "Uncheck non pertinent variable".
<li>Missing variable values for both learning set and predicted objects are replaced by the median value computed from the learning set.
<li>%Pop is the % of populated values for a variable.
<li>Distinct is the number of distinct populated values for a variable
</div>
</div>

<table class="table table-bordered table-condensed" style="width: auto" id="TblVar">
    <tr><th rowspan="2">选择</th>
        <th width="200" rowspan="2" style="text-align: center;">Object variable<br><span style="font-weight: normal">Statistics on 50&nbsp;000 objects</span></th>
        <th width="100" colspan="2" style="text-align: center;line-height: 1;">Learning set </th>
        <th width="100" colspan="2" style="text-align: center;">Objects to predict<br> % Populated</th></tr>
    <tr><th >% Pop.</th><th >Distinct</th><th >ALL</th><th>NOT Validated</th></tr>
    {%   for r in g.critlist %}
        <tr><td> {% if (r[2]>1) %}
            <input type="checkbox" value="Y" name="var{{ r[0] }}" data-var="{{ r[0] }}" >
            {% endif %}
        </td><td>{{ r[0] }}</td>
        <td class="rightfixedfont">{{ r[1] }}</td><td class="rightfixedfont">{{ r[2] }}</td><td class="rightfixedfont">{{ r[3] }}</td><td class="rightfixedfont">{{ r[4] }}</td></tr>
    {% endfor %}
</table>

</td></tr>
</table>
{# ----------- Partie Gestion des Taxon destination --------------------- #}
<a href="#TargetFilter" data-toggle="collapse" > <span class="glyphicon glyphicon-plus-sign"></span> Target Taxonomy Filter</a>
<div id=TargetFilter class="panel panel-default collapse " style="margin-left: 0px;margin-top: 5px;width: 800px;padding: 5px;" >
<div style="margin:5px;">
Predict only the objects of the selected categories from the list below
<br>选择 <a name="tbltargettop" href="#tbltargettop" onclick="$('#TblTargetTaxo input').prop( 'checked', true )">ALL</a>
    / <a href="#tbltargettop" onclick="$('#TblTargetTaxo input').prop( 'checked', false );">None</a>
<table class="table table-bordered table-condensed" style="width: auto" id="TblTargetTaxo">
    <tr><th >选择</th><th width="200" style="text-align: center;">Target Set Category</th></tr>
    {%   for r in g.TargetTaxoList %}
        <tr><td><input type="checkbox" value="Y" name="targettaxo{{ r[0] }}" ></td><td>{{ r[1] }} </td></tr>
    {% endfor %}
</table>
</div></div>

<div class="form-group">
  {{ label("Custom Settings <br>(Taxonomy Post Mapping)"|safe) }}
<div class="col-sm-4">
    <textarea  class="form-control" id="TxtCustSettings" name="TxtCustSettings"  rows="4">{{ g.TxtCustSettings }}</textarea>
</div>
<div class="col-sm-4">
    PostTaxoMapping=PredictecID:TargetId,PredictecID:TargetId,...
</div></div>

    <input type="hidden" name="starttask" value="Y">
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-2">
        <button type="submit" class="btn btn-primary">START Automatic PREDICTION of IDs</button>
    </div>
  </div>
</form>

   <script>
$(document).ready(function() {
        $('#TblVar input').click(ComputeCritVar);
        CheckBasedOnText();

    }); // Ready

 </script>


{% endblock %}